package com.hmkj.quote.job;

import com.hmkj.common.utils.HttpClientUtils;
import com.hmkj.common.utils.StringUtils;
import com.hmkj.core.constant.CacheID;
import com.hmkj.core.service.cache.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 资讯处理定时器
 *
 * @author eproo
 * @create 2017-09-20 14:39
 */
@Slf4j
@Component
public class NewsJob {
    @Resource
    private RedisService redisService;

    /**
     * 资讯查询
     */
    @Scheduled(cron="0 */1 * * * ?")//1分钟执行一次
//    @Scheduled(cron = "0/10 * * * * ? ") // 间隔10秒执行
    public void news() {
        log.info("- - - - - - - - - - - - - -获取Jin10数据- - - - - - - - - - - - - -");
        try {
            String result = StringUtils.decodeUnicode(HttpClientUtils.httpGet("https://m.jin10.com/flash"));
            String result1 = result.substring(1,result.length()-1);
            String[] aaa = result1.split(",");
            List<String> cacheNews = new ArrayList<>();
            for (int i = 0; i <aaa.length ; i++) {
                String sub = aaa[i].substring(1,aaa[i].length()-1);
                if(((!sub.startsWith("0#") && !sub.startsWith("1#") && !sub.endsWith("100")) || sub.contains("href"))){
                    continue;
                }
                String[] resovle = sub.toString().split("#");
                boolean b = false;
                for (int j = 0; j < resovle.length; j++) {
                    if ("0".equals(resovle[0]) && "0".equals(resovle[1])) {//
                        b = true;
                    }
                }
                if(b){
                    continue;
                }
                cacheNews.add(sub);
            }
            redisService.del(CacheID.NEWS);//先删除
            redisService.put(CacheID.NEWS, cacheNews);//再每条逐次放入
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
